/**
* 头部菜单
*/
<template>

  <div>
    <el-menu class="el-menu-demo" mode="horizontal" background-color="#334157" text-color="#fff"
      active-text-color="#fff">
      <el-button class="buttonimg">
        <img class="showimg" :src="collapsed?imgsq:imgshow" @click="toggle(collapsed)">
      </el-button>
      <el-submenu index="2" class="submenu">
        <!-- <template slot="title">{{user.userRealName}}</template> -->
        <template slot="title">
          <span>{{this.userName}}</span>
        </template>
        <el-menu-item @click="modify()" index="2-1">修改密码</el-menu-item>
        <el-menu-item @click="exit()" index="2-2">登出系统</el-menu-item>
      </el-submenu>
    </el-menu>
    <el-dialog :title="title" :visible.sync="modifyVisible" width="30%">
      <el-input type="password" v-model="oldPwd" placeholder="请输入旧密码"></el-input>
      <div style="margin: 20px 0;"></div>
      <el-input type="password" v-model="modifyForm.password" placeholder="请输入新密码"></el-input>
      <div style="margin: 20px 0;"></div>
      <el-button size="small" @click='closeDialog()'>取消</el-button>
      <el-button size="small" type="primary" :loading="loading" class="title" @click="submit()">修改
      </el-button>

    </el-dialog>
  </div>


</template>
<script>
  import {
    loginout,
    userPwd,
    userModifyInfo,
    userInfo
  } from '../api/userMG'
  export default {
    name: 'navcon',
    data() {
      return {
        userName: "",
        title: '修改密码',
        modifyVisible: false, //控制修改密码页面显示与隐藏
        collapsed: true,
        imgshow: require('../assets/img/show.png'),
        imgsq: require('../assets/img/sq.png'),
        user: {},
        oldPwd: "",
        modifyForm: {
          uid: '',
          password: ''
        }
      }
    },
    // 创建完毕状态(里面是操作)
    created() {
      this.user = JSON.parse(localStorage.getItem('userdata'))
      this.modifyForm.uid = this.user.uid
      this.getUserName()
    },
    methods: {
      getUserName() {
        let uid2 = this.user.uid
        userInfo(uid2)
          .then(res => {
            this.userName = res.data.name
          })
          .catch((err) => {
            console.log("Error")
          })
      },
      submit() {
        let data = this.modifyForm
        userModifyInfo(data)
          .then(res => {
            if (res.code == 200) {
              this.modifyVisible = false
              this.modifyForm.password = ""
              this.$message({
                message: '恭喜您，修改密码成功',
                type: 'success'
              });
              this.windows.location.reload()
            }
          })
          .catch(err => {
            console.log("getError")

          })
      },
      modify() {
        this.modifyVisible = true
      },
      // 关闭编辑、增加弹出框
      closeDialog() {
        this.modifyVisible = false
      },
      // 退出登录
      exit() {
        this.$confirm('退出登录, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          })
          .then(() => {
            loginout()
              .then(res => {
                if (res.code == 200) {
                  //如果请求成功就让他2秒跳转路由
                  setTimeout(() => {
                    this.$store.commit('logout', 'false')
                    this.$router.push({
                      path: '/login'
                    })
                    this.$message({
                      type: 'success',
                      message: '已退出登录!'
                    })
                  }, 1000)
                } else {
                  this.$message.error(res.msg)
                  this.logining = false
                  return false
                }
              })
              .catch(err => {
                // 获取图形验证码
                this.getcode()
                this.logining = false
                this.$message.error('退出失败，请稍后再试！')
              })
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消'
            })
          })
      },
      // 切换显示
      toggle(showtype) {
        this.collapsed = !showtype
        this.$root.Bus.$emit('toggle', this.collapsed)
      }
    }
  }

</script>
<style scoped>
  .el-menu-vertical-demo:not(.el-menu--collapse) {
    border: none;
  }

  .submenu {
    float: right;
  }

  .buttonimg {
    height: 60px;
    background-color: transparent;
    border: none;
  }

  .showimg {
    width: 26px;
    height: 26px;
    position: absolute;
    top: 17px;
    left: 17px;
  }

  .showimg:active {
    border: none;
  }

</style>
